<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/7/10
 * Time: 下午7:25
 */

namespace app\api\controller;

use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\Tklog;



class LftOrder extends Pub
{
    public $version="1.0";
//    public $partner_id="18060410173207886";
    public $input_charset="UTF-8";
//    public $core_merchant_no="EW_N5772984681";
    public $sign_type="MD5";
//    public $partner_key="78bc1ac50be17004b58edea930c70c78";
    public $submit_url="http://newfront.liantuobank.com/NewFront/base/gateway.in";
    public function orderreverse($param)
    {
        $User = new Merchant();
        $machinfos = $User->where(array('id' => $param['merchantid']))->find();
        $head["service"] = "front.reverse";//订单撤销
        $head["version"] = $this->version;//默认版本号
        $head["partner_id"] =$machinfos['lft_pid'];//联富通线下提供pid
        $head["input_charset"] = $this->input_charset; //编码格式，默认只支持UTF-8
        $head["core_merchant_no"] = $machinfos['lft_core_merchant_no'];//联富通后台核心商户编号
        $head["sign_type"] = $this->sign_type;//加密方式，默认只支持MD5


        $User = new Merchant();
        $machinfos = $User->where(array('id' => $param['merchantid']))->find();
        $body["out_trade_no"] = $param['out_trade_no'];//商户订单号
        $body["merchant_no"] = $machinfos['lft_merchant_no']; //门店商户编号


        $signArray = array_merge($head, $body);
        $sign = $this->createSign($signArray, $machinfos['lft_key']);
        $head["sign"] = $sign;

        $requestJson["body"] = $body;
        $requestJson["head"] = $head;
        $requestPara["requestJson"] = json_encode($requestJson);
        $resp = $this->requestAsHttpPOST($requestPara, $this->submit_url); //发送请求
        $responseArr = json_decode($resp, true);
        $responseArr=$responseArr['body'];

        if ($responseArr['is_success'] == "S") {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = "";
            $rarr['result_code'] = 'SUCCESS';
            $rarr['result_msg'] = "订单撤销成功";
            $rarr['out_trade_no'] = $param['out_trade_no'];
            $sdata['pay_statu'] = 8;
            $Paylog = new Paylog();
            $Paylog->save($sdata, ['orderid' => $param['out_trade_no']]);

        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['return_msg'] = "";
            $rarr['result_code'] = 'FAIL';
            $rarr['result_msg'] = $responseArr['error'] . $responseArr['message'];
        }
        return $rarr;
    }

    public function orderrefund($param)
    {
        $User = new Merchant();
        $machinfos = $User->where(array('id' => $param['merchantid']))->find();
        $head["service"] = "front.refund";//订单退款
        $head["version"] = $this->version;//默认版本号
        $head["partner_id"] =$machinfos['lft_pid'];//联富通线下提供pid
        $head["input_charset"] = $this->input_charset; //编码格式，默认只支持UTF-8
        $head["core_merchant_no"] = $machinfos['lft_core_merchant_no'];//联富通后台核心商户编号
        $head["sign_type"] = $this->sign_type;//加密方式，默认只支持MD5

        $tk_orderid = $this->getorderid($param['merchantid']);
        $Paylog = new Paylog();
        $order = $Paylog->where('trade_no', $param['out_trade_no'])->find();
        $body["out_trade_no"] = $order['orderid'];//商户订单号
        $body["merchant_no"] = $machinfos['lft_merchant_no'];//门店商户编号
//        $body["trade_no"]            ="00060011806081011119020239178664";//交易流水号,支付请求返回结果参数
        $body["out_refund_no"] = $tk_orderid;//退款订单号
        $body["refund_fee"] = $param['refund_fee'];//退款金额
        $body["refund_reason"] = $param['refund_desc'];//退款的原因说明
        $body["spbill_create_ip"] = "127.0.0.1";//终端IP

        $signArray = array_merge($head, $body);
        $sign = $this->createSign($signArray,$machinfos['lft_key']);
        $head["sign"] = $sign;

        $requestJson["body"] = $body;
        $requestJson["head"] = $head;
        $requestPara["requestJson"] = json_encode($requestJson);
        $resp = $this->requestAsHttpPOST($requestPara, $this->submit_url); //发送请求

        $responseArr = json_decode($resp, true);
        $responseArr = $responseArr['body'];
        if ($responseArr['is_success'] == "S") {
            $rarr['return_code'] = "SUCCESS";
            $rarr['result_code'] = "SUCCESS";
            $rarr['return_msg'] = "退款成功";
            $rarr['result_msg'] = "退款成功";
            $rarr['out_trade_no'] = $order['orderid'];
            $rarr['out_refund_no'] = $responseArr['out_refund_no'];
            $rarr['trade_no'] = $order['trade_no'];
            $rarr['total_fee'] = $order['price'];//支付金额
            $rarr['refund_fee'] = $param['refund_fee'];//退款金额
            $rarr['channel'] = $order['pay_fs'];//支付方式
            $params['tk_status'] = 1;
            $params['sys_orderid'] = $responseArr['refund_trade_no'];
            if ($order['price'] == $order['tk_price'] + $param['refund_fee']) {
                $sedata['pay_statu'] = 2;
                $sedata['tk_status'] = 1;
            } else {
                $sedata['pay_statu'] = 3;
                $sedata['tk_status'] = 2;
            }
            $sedata['tk_price'] = $order['tk_price'] + $param['refund_fee'];
            $Paylog->save($sedata, ['orderid' => $order['orderid']]);
        } elseif ($responseArr['is_success'] == "P") {
            $re_status = $this->order_refund_query($responseArr['out_refund_no'],$param['merchantid']);
            if($re_status['code']=="success"){
                $rarr['return_code'] = "SUCCESS";
                $rarr['result_code'] = "SUCCESS";
                $rarr['return_msg'] = "退款成功";
                $rarr['result_msg'] = "退款成功";
                $rarr['out_trade_no'] = $order['orderid'];
                $rarr['out_refund_no'] = $responseArr['out_refund_no'];
                $rarr['trade_no'] = $order['trade_no'];
                $rarr['total_fee'] = $order['price'];//支付金额
                $rarr['refund_fee'] = $param['refund_fee'];//退款金额
                $rarr['channel'] = $order['pay_fs'];//支付方式
                $params['tk_status'] = 1;
                $params['sys_orderid'] = $responseArr['refund_trade_no'];
                if ($order['price'] == $order['tk_price'] + $param['refund_fee']) {
                    $sedata['pay_statu'] = 2;
                    $sedata['tk_status'] = 1;
                } else {
                    $sedata['pay_statu'] = 3;
                    $sedata['tk_status'] = 2;
                }
                $sedata['tk_price'] = $order['tk_price'] + $param['refund_fee'];
                $Paylog->save($sedata, ['orderid' => $order['orderid']]);
            }elseif($re_status['code']=="fail"){
                $rarr['return_code'] = "SUCCESS";
                $rarr['result_code'] = "FAIL";
                $rarr['return_msg'] = $re_status['msg'];
                $rarr['result_msg'] = $re_status['msg'];
                $params['tk_status'] = 3;
            }

        } else {
            $rarr['return_code'] = "SUCCESS";
            $rarr['result_code'] = "FAIL";
            $rarr['return_msg'] = $responseArr['error'] . $responseArr['message'];
            $rarr['result_msg'] = $responseArr['error'] . $responseArr['message'];
            $params['tk_status'] = 3;
        }
        $params['tk_info'] = json_encode($responseArr);
        $Tklog = new Tklog();

        $Tklog->save($params);
        return $rarr;
    }

    public function order_refund_query($out_refund_no,$merchantid)
    {

        $User = new Merchant();
        $machinfos = $User->where(array('id' => $merchantid))->find();
        $head["service"] = "front.refundquery";//订单退款
        $head["version"] = $this->version;//默认版本号
        $head["partner_id"] = $machinfos['lft_pid'];//联富通线下提供pid
        $head["input_charset"] = $this->input_charset; //编码格式，默认只支持UTF-8
        $head["core_merchant_no"] = $machinfos['lft_core_merchant_no'];//联富通后台核心商户编号
        $head["sign_type"] = $this->sign_type;//加密方式，默认只支持MD5
        $body["out_refund_no"] = $out_refund_no;//退款订单号
        $signArray = array_merge($head, $body);
        $sign = $this->createSign($signArray, $machinfos['lft_key']);
        $head["sign"] = $sign;

        $requestJson["body"] = $body;
        $requestJson["head"] = $head;
        $requestPara["requestJson"] = json_encode($requestJson);
        $resp = $this->requestAsHttpPOST($requestPara, $this->submit_url); //发送请求

        $responseArr = json_decode($resp, true);
        $responseArr = $responseArr['body'];
        if ($responseArr['is_success'] == "S") {
            if ($responseArr['refund_status'] == "success") {
                $data['code'] = "success";
                $data['msg'] = "success";
            } elseif ($responseArr['refund_status'] == "refunding") {
                $data['code'] = "fail";
                $data['msg'] = "退款中";
            } elseif ($responseArr['refund_status'] == "fail") {
                $data['code'] = "fail";
                $data['msg'] = $responseArr['refund_error_code'].$responseArr['refund_error_msg'];
            }
        }else{
            $data['code']="fail";
            $data['msg']= $responseArr['error'] . $responseArr['message'];
        }
        return $data;



    }

}